<?php
namespace App\Services;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

/**
 *
 * 配置服务层
 *
 * @author maqu
 *
 */
class ConfigService extends BaseService {

    public function getAllConfigs(){

        $key = 'maqu.data.config';
        $configs = Cache::get($key);

        if(!$configs){

            $datas = DB::table('config')
                ->where('parent_id','>',0)
                ->select('code','value')
                ->get();

            $configs =[];
            foreach($datas as $item){
                $configs[$item->code]=$item->value;
            }

            Cache::put($key,$configs,2*60);
        }

        return $configs;

    }

    public function getConfig($name){

        $key = 'maqu.data.config';
        $configs = Cache::get($key);

        if(!$configs){

            $datas = DB::table('config')
                ->where('parent_id','>',0)
                ->select('code','value')
                ->get();

            $configs =[];
            foreach($datas as $item){
                $configs[$item->code]=$item->value;
            }

            Cache::put($key,$configs,2*60);
        }

        if(array_key_exists($name,$configs)){
            return $configs[$name];
        } else {
            return null;
        }

    }

    public function fogetConfig(){
        $key = 'maqu.data.config';
        Cache::forget($key);
    }

}